<?php
namespace Core\Component;
use \Phalcon\DI;
use \Phalcon\Mvc\User\Component;

/**
 * Installer.
 */
abstract class Installer extends Component
{
    /**
     * Used to install specific database entities or other specific action.
     *
     * @return void
     */
    public abstract function install();

    /**
     * Used before package will be removed from the system.
     *
     * @return void
     */
    public abstract function remove();

    /**
     * Used to apply some updates.
     *
     * @param string $currentVersion Current version name.
     *
     * @return mixed 'string' (new version) if migration is not finished, 'null' if all updates were applied
     */
    public abstract function update($currentVersion);

    /**
     * Execute sql file.
     *
     * @param string $filePath SQL file path.
     *
     * @throws Exception
     * @return void
     */
    public function runSqlFile($filePath)
    {
        if (file_exists($filePath)) {
            $connection = $this->getDI()->get('db');
            $connection->begin();
            $connection->query(file_get_contents($filePath));
            $connection->commit();
        } else {
            throw new Exception(sprintf('Sql file "%s" does not exists', $filePath));
        }
    }
}